/**
 * Copyright (C) 2007-2011 JUG Events Team <info@jugevents.org>
 *
 * This file is part of JUG Events - Core.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jugevents.core.util;



import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;

import org.joda.time.DateMidnight;



public class Utilities {
	 public static boolean todayIsBeforeDate(final Date date) {
	        boolean result;
	        // old way
	        DateMidnight today = new DateMidnight();
	        result = today.compareTo(new DateMidnight(date)) <= 0;
	        return result;
	    }
	 
	 /**
	     * Append a parameter to an URL.
	     * 
	     * @param sb The StringBuilder containing the URL where to append.
	     * @param parameterName The name of the parameter
	     * @param parameterValue The value of the parameter. It will be url encoded.
	     * @param ifNotNull if true the parameter is appended only if its vale is not null.
	     * @return The passed StringBuilder
	     */
	    public static StringBuilder appendUrlParameter(StringBuilder sb,
	            String parameterName, String parameterValue, boolean ifNotNull)
	            throws UnsupportedEncodingException {
	        if (ifNotNull && parameterValue == null) {
	            return sb;
	        }
	        if (sb.indexOf("?") == -1) {
	            sb.append('?');
	        }
	        if (sb.charAt(sb.length() - 1) != '?') {
	            sb.append('&');
	        }
	        sb.append(parameterName).append('=');
	        if (parameterValue != null) {
	            sb.append(URLEncoder.encode(parameterValue, "UTF-8"));
	        }
	        return sb;
	    }
	    /**
	     * Reads a resource as stream and converts it into an array of bytes
	     * @param path path of the resource.
	     * @return
	     * @throws IOException
	     */
	    public static byte[] resourceToBytes(String path) throws IOException {
	    	InputStream is = null;
	    	ByteArrayOutputStream out = null;
	    	byte[] buffer = new byte[1024];
	    	int len;
	    	try {
	    		is = Utilities.class.getResourceAsStream(path);        	
	        	out = new ByteArrayOutputStream(buffer.length);   
	        	while((len = is.read(buffer)) >= 0) {
	        		out.write(buffer, 0, len);
	        	}
	        	return out.toByteArray();
	            	
			}
			finally {
				is.close();
		    	out.close();
			}    	
	    	} 
	

}
